//  
//  BrowseFolder.cs
//  
//  Author:
//       Brian L. Crawford <kg4mfq@gmail.com>
// 
//  Copyright (c) 2011 Brian L. Crawford
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
//using System.Drawing;
using System.Windows.Forms;

namespace FileCompare
{
	public class BrowseFolder
	{
		FolderBrowserDialog dialog;
		OpenFileDialog fdialog;
        private bool _result;

        public bool Result { get { return _result; } }

		public BrowseFolder()
		{
			dialog = new FolderBrowserDialog();
			fdialog = new OpenFileDialog();
		}
		
		public string GetBrowse(string Title, string startpath)
		{
			dialog.Description = Title;
			dialog.SelectedPath = startpath;
			dialog.ShowNewFolderButton = true;
			DialogResult result = dialog.ShowDialog();
			if (result == DialogResult.OK)
			{
                _result = true;
				return dialog.SelectedPath;
			}
			else
			{
                _result = false;
                return "";
			}
		}
		public string GetBrowseFiles(string title, string startpath, bool AllowMultipleFileSelection,string filefilter)
		{
			fdialog.Title = title;
			fdialog.Multiselect = AllowMultipleFileSelection;
			fdialog.SupportMultiDottedExtensions = true;
			
			if (startpath == null)
			{
				fdialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
			}
			else if (startpath == "")
			{
				fdialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
			}
			else
			{
				fdialog.InitialDirectory = startpath;
			}
			
			fdialog.Filter = filefilter;
			fdialog.FilterIndex = 1;
			fdialog.RestoreDirectory = true;
			
			if (fdialog.ShowDialog() == DialogResult.OK)
			{
				try
				{
					startpath =  fdialog.FileName;
				}
				catch (Exception ex)
				{
					MessageBox.Show("Can't read that file. Here's why: " + ex.Message);
				}
			}

            if (startpath != null)
            {
                _result = true;
                return startpath;
            }
            else
            {
                _result = false;
                return null;
            }
		}
			
	}
}

